<template>
  <a-form-model
    layout="horizontal"
    :model="formInline"
    @submit="handleSubmit"
    @submit.native.prevent
  >
    <a-form-model-item>
      <a-input v-model="formInline.user" placeholder="请输入账号">
        <a-icon slot="prefix" type="user" style="color: rgba(0, 0, 0, 0.25)" />
      </a-input>
    </a-form-model-item>
    <a-form-model-item>
      <a-input
        v-model="formInline.pswd"
        type="password"
        placeholder="请输入密码"
      >
        <a-icon slot="prefix" type="lock" style="color: rgba(0, 0, 0, 0.25)" />
      </a-input>
    </a-form-model-item>
    <a-form-model-item>
      <a-button
        type="primary"
        html-type="submit"
        :disabled="formInline.user === '' || formInline.pswd === ''"
        block
      >
        添加账号
      </a-button>
    </a-form-model-item>
  </a-form-model>
</template>
<script>
export default {
  data() {
    return {
      formInline: {
        user: "",
        pswd: "",
      },
    };
  },
  methods: {
    handleSubmit() {
      let that = this;
      //读入配置文件
      this.readFile((data) => {
        let status = true;
        data.userInfo.forEach((item) => {
          if (item.user === that.formInline.user) {
            item.pswd = that.formInline.pswd;
            status = false;
          }
        });
        if (status) {
          data.userInfo.push({
            ...that.formInline,
          });
        }
        console.log(data, "读入");
        //追加配置文件
        that.writeFile({ ...data }, () => {
          that.$emit("getTnfo", that.formInline);
        });
      });
    },
  },
};
</script>
<style lang="less" scoped>
</style>
